package com.example.spinning.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spinning.bean.Dyeing;
import com.example.spinning.bean.DyeingVo;
import com.example.spinning.bean.PaginationResult;
import com.example.spinning.mapper.DyeingMapper;
import com.example.spinning.utils.CraftManagementTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author bayernxmt
 * @create 2020-11-11-11:12
 */
@Service
@Transactional
public class DyeingService {

    @Autowired
    private DyeingMapper dyeingMapper;

    //查询所有 并且分页展示 : current表示当前页，size表示一页展示多少条数据
    public PaginationResult<Dyeing> selectAll(int current, int size){
        Page<Dyeing> pages = new Page<>(current,size);
        dyeingMapper.selectPage(pages, null);
        List<Dyeing> list = pages.getRecords();
        int total = dyeingMapper.selectCount(null);

        PaginationResult<Dyeing> pr = new PaginationResult<>();
        pr.setTotal(total);
        pr.setList(list);
        return pr;
    }

    //单、多条件查询 并且实现分页
    public PaginationResult<Dyeing> selectByMulParams(int current, int size, Dyeing dyeing,String dateTime){

        QueryWrapper<Dyeing> wrapper = new QueryWrapper();
        if (null!=dyeing.getBatch_no())
            wrapper.eq("batch_no", dyeing.getBatch_no());
        if (null!=dyeing.getName())
            wrapper.eq("name", dyeing.getName());
        if (null!=dateTime)
            wrapper.like("date", dateTime);

        Page<Dyeing> pages = new Page<>(current,size);
        dyeingMapper.selectPage(pages, wrapper);
        int total = dyeingMapper.selectCount(wrapper);
        List<Dyeing> list = pages.getRecords();

        PaginationResult<Dyeing> pr = new PaginationResult<>();
        pr.setTotal(total);
        pr.setList(list);
        return pr;
    }

    //根据id查询
    public Dyeing SelectById(int id){
        return dyeingMapper.selectById(id);
    }

    //插入数据
    public int insert(Dyeing dyeing) {
        dyeing.setDate(CraftManagementTimeUtils.getTimestamp());
        int result = dyeingMapper.insert(dyeing); //result为受到影响的条数
        return result;
    }


    //批量删除数据（与删除一条合并）
    public int delete(List<Integer> ids){
        int result = dyeingMapper.deleteBatchIds(ids);
        return result;
    }

    //修改数据(已经实现了动态sql)
    public int update(Dyeing dyeing){
        dyeing.setDate(new Timestamp(new Date().getTime()));
        int result = dyeingMapper.updateById(dyeing);
        return result;
    }

    //内外表一对多查询时，封装数据
    public List<DyeingVo> getDyeingVoList(List<Dyeing> dyeingList){
        List<DyeingVo> dyeingVoList = new ArrayList<>();

        for (Dyeing element:dyeingList) {
            DyeingVo dyeingVo = new DyeingVo();
            dyeingVo.setId(element.getId());
            dyeingVo.setBatch_no(element.getBatch_no());
            dyeingVo.setName(element.getName());
            dyeingVo.setDate(element.getDate());
            dyeingVo.setDyelot_no(element.getDyelot_no());
            dyeingVo.setMaterial(element.getMaterial());
            dyeingVo.setColor(element.getColor());
            dyeingVo.setCustomer(element.getCustomer());
            dyeingVo.setDyelot_num(element.getDyelot_num());
            dyeingVo.setBath_ratio(element.getBath_ratio());
            dyeingVo.setTemperature1(element.getTemperature1());
            dyeingVo.setTemperature2(element.getTemperature2());
            dyeingVo.setTemperature3(element.getTemperature3());

            dyeingVoList.add(dyeingVo);
        }
        return dyeingVoList;
    }

}
